package com.question.business.dept.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.question.business.dept.model.vo.DeptDetailVO;
import com.question.business.dept.service.IDeptService;
import com.question.common.annotation.DataIgnoreConvert;
import com.question.common.annotation.DeptConvert;
import com.question.common.annotation.DeptIgnoreConvert;
import com.question.common.enums.DataIgnoreConvertEnum;
import com.question.common.model.dto.DataTypeConvertDTO;
import com.question.common.service.IDataConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * 部门数据转换
 *
 * @author star
 * @since 2024/4/3
 */
@Slf4j
@Service
public class DeptDataConvertImpl implements IDataConvert {

    @Resource
    private IDeptService deptService;

    @Override
    public String convert(String sourceFieldName, Annotation annotation, Object value) {
        if (Objects.nonNull(value)) {
            StringJoiner joiner = new StringJoiner(",");
            if (value instanceof List) {
                List list = (List) value;
                for (Object id : list) {
                    DeptDetailVO dept = deptService.queryDeptDetail(String.valueOf(id));
                    if (Objects.nonNull(dept) && StringUtils.isNotBlank(dept.getDeptName())) {
                        joiner.add(dept.getDeptName());
                    }
                }
            } else {
                DeptDetailVO dept = deptService.queryDeptDetail(String.valueOf(value));
                if (Objects.nonNull(dept) && StringUtils.isNotBlank(dept.getDeptName())) {
                    joiner.add(dept.getDeptName());
                }
            }
            return joiner.toString();
        }
        return null;
    }

    @Override
    public DataTypeConvertDTO confirmType(Field field) {
        DeptConvert deptConvert = field.getAnnotation(DeptConvert.class);
        if (Objects.nonNull(deptConvert)) {
            String targetFieldName = deptConvert.target();
            if (StringUtils.isBlank(targetFieldName)) {
                targetFieldName = field.getName().concat("Name");
            }
            return DataTypeConvertDTO.builder()
                    .annotation(deptConvert)
                    .dataConvert(this)
                    .targetField(targetFieldName)
                    .build();
        }
        return null;
    }

    @Override
    public boolean isIgnore(DataIgnoreConvertEnum ignoreEnum, Field sourceField) {
        DataIgnoreConvert dataIgnoreConvert = sourceField.getAnnotation(DataIgnoreConvert.class);
        boolean isDataIgnore = Objects.nonNull(dataIgnoreConvert) && DataIgnoreConvertEnum.DEPT.equals(dataIgnoreConvert.type());
        DeptIgnoreConvert deptIgnoreConvert = sourceField.getAnnotation(DeptIgnoreConvert.class);
        return DataIgnoreConvertEnum.DEPT.equals(ignoreEnum) || isDataIgnore || Objects.nonNull(deptIgnoreConvert);
    }
}
